home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kopete / kopetetask.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  5.2 KB  |  163 lines

  1. /*
  2.     kopetetask.h - Kopete Task
  3.  
  4.     Copyright (c) 2004      by Richard Smith         <kde@metafoo.co.uk>
  5.     Kopete    (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
  6.  
  7.     *************************************************************************
  8.     *                                                                       *
  9.     * This library is free software; you can redistribute it and/or         *
  10.     * modify it under the terms of the GNU Lesser General Public            *
  11.     * License as published by the Free Software Foundation; either          *
  12.     * version 2 of the License, or (at your option) any later version.      *
  13.     *                                                                       *
  14.     *************************************************************************
  15. */
  16.  
  17. #ifndef KOPETETASK_H
  18. #define KOPETETASK_H
  19.  
  20. #include <qobject.h>
  21. #include <kdemacros.h>
  22.  
  23. namespace Kopete
  24. {
  25.  
  26. /**
  27.  * The base class for all tasks.
  28.  * For most tasks created in Kopete, the code looks like
  29.  *
  30.  * \code
  31.  *   Kopete::Task *task = someobject->someoperation( some parameters );
  32.  *   connect( task, SIGNAL( result( Kopete::Task * ) ),
  33.  *            this, SLOT( slotResult( Kopete::Task * ) ) );
  34.  * \endcode
  35.  *   (other connects, specific to the job)
  36.  *
  37.  * And slotResult is usually at least:
  38.  *
  39.  * \code
  40.  *  if ( !task->succeeded() )
  41.  *      Kopete::UI::Global::showTaskError( task );
  42.  * \endcode
  43.  *
  44.  * Much of the ideas (and some of the documentation and function names) for this
  45.  * class come from KIO::Job.
  46.  * @author Richard Smith <kde@metafoo.co.uk>
  47.  */
  48. class Task : public QObject
  49. {
  50.     Q_OBJECT
  51.  
  52. protected:
  53.     Task();
  54. public:
  55.     ~Task();
  56.  
  57.     /**
  58.      * Returns whether the task completed successfully.
  59.      * Only call this method from the slot connected to result().
  60.      * @return if the task succeeded, returns true, otherwise returns false.
  61.      */
  62.     bool succeeded() const;
  63.     /**
  64.      * Converts an error code and a non-i18n error message into an
  65.      * error message in the current language. The low level (non-i18n)
  66.      * error message (usually a url) is put into the translated error
  67.      * message using %%1.
  68.      *
  69.      * Use this to display the error yourself, but for a dialog box
  70.      * use Kopete::UI::Global::showTaskError. Do not call it if succeeded()
  71.      * returns true.
  72.      * @return the error message and if there is no error, a message
  73.      *         telling the user that the app is broken, so check with
  74.      *         succeeded() whether there is an error.
  75.      */
  76.     const QString &errorString() const;
  77.  
  78.     /** Flags for the abort() function */
  79.     enum AbortFlags { AbortNormal = 0, AbortEmitResult = 1 };
  80. public slots:
  81.     /**
  82.      * Abort this task.
  83.      * This aborts all subtasks and deletes the task.
  84.      *
  85.      * @param flags a combination of flags from AbortFlags. If AbortEmitResult is
  86.      *        set, Job will emit the result signal. AbortEmitResult is removed
  87.      *        from the flags passed to the abort function of subtasks.
  88.      */
  89.     virtual void abort( int flags = AbortNormal );
  90.  
  91. signals:
  92.     /**
  93.      * Emitted when the task is finished, in any case (completed, canceled,
  94.      * failed...). Use error() to find the result.
  95.      * @param task the task that emitted this signal
  96.      */
  97.     void result( Kopete::Task *task );
  98.     /**
  99.      * Emitted to display status information about this task.
  100.      * Examples of messages are:
  101.      *   "Removing ICQ contact Joe from server-side list",
  102.      *   "Loading account plugin", etc.
  103.      * @param task the task that emitted this signal
  104.      * @param message the info message
  105.      */
  106.     void statusMessage( Kopete::Task *task, const QString &message );
  107.  
  108. protected:
  109.     /**
  110.      * Add a task that has to be completed before a result is emitted. This
  111.      * obviously should not be called after the finish signal is emitted by
  112.      * the subtask.
  113.      *
  114.      * @param task the subtask to add
  115.      */
  116.     virtual void addSubtask( Task *task );
  117.  
  118.     enum RemoveSubtaskIfLast { IfLastDoNothing, IfLastEmitResult };
  119.     /**
  120.      * Mark a sub job as being done. If it's the last to
  121.      * wait on the job will emit a result - jobs with
  122.      * two steps might want to override slotResult
  123.      * in order to avoid calling this method.
  124.      *
  125.      * @param task the subjob to add
  126.      * @param actionIfLast the action to take if this is the last subtask.
  127.      *        If set to IfLastEmitResult, the error information from @p task
  128.      *        will be copied to this object, and emitResult() will be called.
  129.      */
  130.     virtual void removeSubtask( Task *task, RemoveSubtaskIfLast actionIfLast = IfLastEmitResult );
  131.  
  132.     enum Result { ResultFailed = 0, ResultSucceeded = 1 };
  133.     /**
  134.      * Utility function to emit the result signal, and suicide this job.
  135.      * Sets the stored result and error message to @p result and @p errorMessage.
  136.      * You should call this instead of emitting the result() signal yourself.
  137.      */
  138.     void emitResult( Result result = ResultSucceeded, const QString &errorMessage = QString::null );
  139.  
  140. protected slots:
  141.     /**
  142.      * Called whenever a subtask finishes.
  143.      * The default implementation checks for errors and propagates
  144.      * them to this task, then calls removeSubtask().
  145.      * Override if you want to provide a different @p actionIfLast to
  146.      * removeSubtask, or want to perform some other processing in response
  147.      * to a subtask finishing
  148.      * @param task the subtask that finished
  149.      * @see result()
  150.      */
  151.     virtual void slotResult( Kopete::Task *task );
  152.  
  153. private:
  154.     class Private;
  155.     Private *d;
  156. };
  157.  
  158. }
  159.  
  160. #endif
  161.  
  162. // vim: set noet ts=4 sts=4 sw=4:
  163.